package com.pignest.hotel.module.global;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.MemberLevel;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class MemberLevelSetWindow extends Window {
 
	private static final long serialVersionUID = 2147239867923279156L;
	private String classid;
	private MemberLevel memberLevel;
	
	private Textbox memberleveField;//会员等级
	private Textbox overdraftField;//可透支额度
	private Listbox selfhelpliveListbox;//是否可自助入住
	private Listbox selfhelpoutListbox;//是否可自助退房
	private Textbox levelPriceField;//等级所需金额:
	//private Textbox levelIntegalField;//等级所需积分
	private Textbox levelPay;//等级所需支付金额


	private void init(){
		
		memberleveField = (Textbox)getFellow("memberleveField");
		overdraftField = (Textbox)getFellow("overdraftField");
		selfhelpliveListbox=(Listbox)getFellow("selfhelpliveListbox");
		selfhelpoutListbox=(Listbox)getFellow("selfhelpoutListbox");
		levelPriceField=(Textbox)getFellow("levelPriceField");
		//levelIntegalField=(Textbox)getFellow("levelIntegalField");
		levelPay =(Textbox)getFellow("levelPay");
	}
	public String save(){
		init();
		
		String levelName = memberleveField.getValue();
		if (StringUtils.isEmpty(levelName)) {
			return "会员等级不能为空!";
		 }
		
        if(!Utils.checkPrice(overdraftField.getValue())){
			overdraftField.focus();
			return "输入的数字不合法：非负正数!";
		}
		MemberService server =Services.getMemberService();
		MemberLevel level= new MemberLevel();
		level.setMemberlevel(levelName);
		int over =Utils.toPrice(overdraftField.getValue());
		int levelPrice =Utils.toPrice(levelPriceField.getValue());
          level.setLevelPrice(levelPrice);
		  level.setOverdraft(over);

		String selfhelplive= UIUtils.getSelectedValue(selfhelpliveListbox);
		 level.setSelfhelplive(selfhelplive);
	    String selfhelpout=UIUtils.getSelectedValue(selfhelpoutListbox);
		 level.setSelfhelpout(selfhelpout);
		int pay =Utils.toPrice(levelPay.getValue());
		level.setLevelPay(pay);
		level.setLevelIntegal(0);
		//int levelIntegal=levelIntegalField.getValue();
		 //level.setLevelIntegal(levelIntegal);
		 if (StringUtils.isEmpty(classid)) {//新增
				 if (server.isExistMemberLevel(levelName,null)){
				     return "已有存在该等级";
			     }
				 server.addMemberLevel(level);
				
			 }else{
				 level.setId(classid);
				 if (server.isExistMemberLevel(levelName,classid)){
				     return "已有存在该等级";
			     }
				 server.updateMemberLevel(level);
			 }
		 UIUtils.refresh();
		return "";
	  }
	 public void setMemberLevelId(String id){
		 
		 try{
			 if(id!=null){
				 MemberLevel level =Services.getMemberService().getMemberLevel(id);
			 if(level!=null){
				 this.classid=id;
				 this.memberLevel=level;
			  }
			 }
			 setMemberLevel(memberLevel);
			 }catch(Exception e){
				 e.printStackTrace();
				 
			 }
		 }
		private void setMemberLevel(MemberLevel level){
			init();
			memberleveField.setValue(level.getMemberlevel());
			Integer over =level.getOverdraft();
			if(over!=null)
			overdraftField.setValue(Utils.toPrice(level.getOverdraft()) );
			UIUtils.setSelectedItem(selfhelpliveListbox, level.getSelfhelplive());
			UIUtils.setSelectedItem(selfhelpoutListbox, level.getSelfhelpout());
			levelPriceField.setValue(Utils.toPrice(level.getLevelPrice()));
			//levelIntegalField.setValue(level.getLevelIntegal());
	 }
}
